package cn.iocoder.yudao.module.station.controller.admin.fillrecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.station.controller.admin.fillrecord.vo.*;
import cn.iocoder.yudao.module.station.dal.dataobject.fillrecord.FillRecordDO;
import cn.iocoder.yudao.module.station.convert.fillrecord.FillRecordConvert;
import cn.iocoder.yudao.module.station.service.fillrecord.FillRecordService;

@Tag(name = "管理后台 - 充装记录")
@RestController
@RequestMapping("/station/fill-record")
@Validated
public class FillRecordController {

    @Resource
    private FillRecordService fillRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建充装记录")
    @PreAuthorize("@ss.hasPermission('station:fill-record:create')")
    public CommonResult<Long> createFillRecord(@Valid @RequestBody FillRecordCreateReqVO createReqVO) {
        return success(fillRecordService.createFillRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新充装记录")
    @PreAuthorize("@ss.hasPermission('station:fill-record:update')")
    public CommonResult<Boolean> updateFillRecord(@Valid @RequestBody FillRecordUpdateReqVO updateReqVO) {
        fillRecordService.updateFillRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除充装记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('station:fill-record:delete')")
    public CommonResult<Boolean> deleteFillRecord(@RequestParam("id") Long id) {
        fillRecordService.deleteFillRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得充装记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('station:fill-record:query')")
    public CommonResult<FillRecordRespVO> getFillRecord(@RequestParam("id") Long id) {
        FillRecordDO fillRecord = fillRecordService.getFillRecord(id);
        return success(FillRecordConvert.INSTANCE.convert(fillRecord));
    }

    @GetMapping("/list")
    @Operation(summary = "获得充装记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('station:fill-record:query')")
    public CommonResult<List<FillRecordRespVO>> getFillRecordList(@RequestParam("ids") Collection<Long> ids) {
        List<FillRecordDO> list = fillRecordService.getFillRecordList(ids);
        return success(FillRecordConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得充装记录分页")
    @PreAuthorize("@ss.hasPermission('station:fill-record:query')")
    public CommonResult<PageResult<FillRecordRespVO>> getFillRecordPage(@Valid FillRecordPageReqVO pageVO) {
        PageResult<FillRecordDO> pageResult = fillRecordService.getFillRecordPage(pageVO);
        return success(FillRecordConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出充装记录 Excel")
    @PreAuthorize("@ss.hasPermission('station:fill-record:export')")
    @OperateLog(type = EXPORT)
    public void exportFillRecordExcel(@Valid FillRecordExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<FillRecordDO> list = fillRecordService.getFillRecordList(exportReqVO);
        // 导出 Excel
        List<FillRecordExcelVO> datas = FillRecordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "充装记录.xls", "数据", FillRecordExcelVO.class, datas);
    }

}
